package kr.ac.konkuk.db.quadtree.impl;

import java.util.List;
import java.util.Set;

import kr.ac.konkuk.db.quadtree.QuadTree;
import kr.ac.konkuk.db.quadtree.RowKey;
import kr.ac.konkuk.db.quadtree.callback.SearchOperator;

import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

public class WKTQuadTree implements QuadTree<String> {
	private QuadTree<Geometry> qt;
	private WKTReader reader = new WKTReader();
	
	public WKTQuadTree(QuadTree<Geometry> qt) {
		this.qt = qt;
	}

	public Set<String> insert(RowKey rowKey, String item) {
		return qt.insert(rowKey, read(item));
	}

	public Set<String> insert(List<Byte> rowKey, String item) {
		return qt.insert(rowKey, read(item));
	}

	public Set<String> search(Envelope searchEnv, SearchOperator operator) {
		return qt.search(searchEnv, operator);
	}

	public boolean remove(RowKey rowKey) {
		return qt.remove(rowKey);
	}

	public boolean remove(List<Byte> rowKey) {
		return qt.remove(rowKey);
	}
	
	private Geometry read(String item) {
		try {
			return reader.read(item);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public Set<String> search(Geometry searchEnv, SearchOperator operator) {
		return qt.search(searchEnv, operator);
	}
}
